# server.py
from flask import Flask, request, jsonify
from loguru import logger
import reque_pb2
import math_problems_new
import poems_problems
import English_problems
import AI_problems
import image_text

app = Flask(__name__)

@app.route('/get_questions', methods=['GET', 'POST'])
def get_questions():
    try:
        data = request.get_json()

        grade = data['keyword']['grade']
        subject = data['keyword']['subject']
        topic = data['keyword']['topic']
        difficulty = data['keyword']['difficulty']
        quantity = data['quantity']
        difficulty_list = ['easy', 'midum', 'hard']
        Grades = ['一年级', '二年级', '三年级', '四年级', '五年级', '六年级']
        if subject == '数学' and topic in [0, 1, 2, 3] and difficulty in difficulty_list and grade in Grades:
            question = math_problems_new.get_request(grade, int(topic), quantity, difficulty)
        elif subject == '语文' and topic in [0, 1, 2] and difficulty in difficulty_list and grade in Grades:
            question = poems_problems.get_request(grade, int(topic), quantity, difficulty)
        elif subject == '英语' and topic in [0, 1, 2] and difficulty in difficulty_list and grade in Grades[2:]:
            question = English_problems.get_request(grade, topic, quantity, difficulty)
        elif subject in ['英语', '语文'] and topic == 3 and difficulty == 'hard':
            question = AI_problems.get_request(subject, grade, topic, quantity, difficulty)
        elif subject == 'magic' and topic in [0, 1, 2]:
            question = image_text.get_request(topic, quantity)
        else:
            question = [{'question_text': "问题请求错误", 'answer': "暂无答案"}]
        #
        # result = {
        #     'questions': question
        # }
        result = question

        return jsonify(result)

    except Exception as e:
        logger.error(str(e))
        return jsonify({'error': 'An error occurred'}), 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=9000)
